package cn.sytton.taffe.web.flw.service.impl;

import cn.sytton.taffe.web.flw.entity.FormDefEntity;
import cn.sytton.taffe.web.flw.entity.FormDefStructureEntity;
import cn.sytton.taffe.web.flw.mapper.FormDefMapper;
import cn.sytton.taffe.web.flw.service.IFormDefService;
import cn.sytton.taffe.framework.base.service.BaseService;
import cn.sytton.taffe.web.flw.service.IFormDefStructureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 工作流表单表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-09-26
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class FormDefService extends BaseService<FormDefMapper, FormDefEntity> implements IFormDefService {

    private final IFormDefStructureService formDefStructureService;

    @Override
    public void createOrUpdate(FormDefEntity entity) {
        saveOrUpdate(entity);
        if (!StringUtils.hasText(entity.getStructureId())) {
            FormDefStructureEntity structure = formDefStructureService.create(entity.getFormId());
            entity.setStructureId(structure.getStructureId());
            update(entity);
        }
    }

    @Override
    public void delete(String... ids) {
        super.delete(ids);
        formDefStructureService.removeByFormIds(ids);
        //todo 流程是否删除
        //todo 流程实例是否删除
    }

    @Override
    public FormDefEntity findById(String id) {
        return baseMapper.findById(id);
    }
}
